package com.random.fox.whatspay.utils;

import android.content.Context;
import android.graphics.Paint;

/**
 * Created by 渠 on 2014/9/1.
 */
public class Utils {

    /**
     * dip转px
     */
    public static int getPxForDip(Context context, int dip){
        return (int)(dip * (context.getResources().getDisplayMetrics().densityDpi / 160.0F));
    }

    /**
     * sp转px
     */
    public static int getPxForSp(Context context, float sp){
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (sp * fontScale + 0.5f);
    }

    public static int getTextWidth(Paint paint, String str) {
        int iRet = 0;
        if (str != null && str.length() > 0) {
            int len = str.length();
            float[] widths = new float[len];
            paint.getTextWidths(str, widths);
            for (int j = 0; j < len; j++) {
                iRet += (int) Math.ceil(widths[j]);
            }
        }
        return iRet;
    }
}
